/*
 * todo methods are data; make all methods static to save memory!
 */

/**
 * create ns if not already done
  */
var net = net || {};
net.andersand = net.andersand || {};
net.andersand.arcomage = net.andersand.arcomage || {};

/**
 * Instance of a card rule, which is a definition
 * @param rule
 * @param {number} index
 */
net.andersand.arcomage.Card = function(rule, index) {

    var ruleIndex = index;

    this.getRuleIndex = function() {
        return ruleIndex;
    };

    // copy all own properties from rule
    for (var key in rule) {
        if (rule.hasOwnProperty(key)) {
            this[key] = rule[key];
        }
    }

};

net.andersand.arcomage.Game = function () {
    /** Array of Cards */
    var deck = [];
    /** Array of Players */
    this.players = [];
    var currentPlayer = 0;

    var self = this;
    var logger = net.andersand.util.logger;
    var arcomage = net.andersand.arcomage;
    this.config = arcomage.config;
    this.rulebase = arcomage.rulebase;
    this.gui = null;

    this.getCurrentPlayer = function() {
        return self.players[currentPlayer];
    };
    this.getDeck = function() {
        return deck;
    };
    this.nextTurn = function() {
        currentPlayer++;
        if (currentPlayer > self.players.length) {
            currentPlayer = 0;
        }
    };
    this.handleTurn = function() {

    };
    this.createDeck = function() {
        var createCardsFromRule = function(rule, ruleIndex) {
            rule.quantity = rule.quantity || 1;
            rule.name = rule.name || "void";
            if (rule.name.toLowerCase().indexOf("void") < 0) {
                for (var i=0; i<rule.quantity; i++) {
                    deck.push(new arcomage.Card(rule, ruleIndex));
                }
            }
        };
        for (var key in self.rulebase) {
            createCardsFromRule(self.rulebase[key][0], 0);
            createCardsFromRule(self.rulebase[key][1], 1);
        }
    };
    this.init = function() {
        self.gui = new arcomage.GUI({"game" : self});
        self.gui.init();
    };
};

net.andersand.arcomage.Player = function (options) {
    options = options || {};

    /** function or null */
    this.ai = options.ai;
    this.name = options.name || this.ai ? "Enemy" : "Player";

    this.cards = [];
    this.tower = (typeof options.tower == "number") ? options.tower : 15;
    this.wall = options.wall || 5;

    this.quarry = options.quarry || 3;
    this.magic = options.magic || 3;
    this.dungeon = options.dungeon || 3;

    this.bricks = options.bricks || 10;
    this.gems = options.gems || 10;
    this.recruits = options.recruits || 10;

    var self = this;

    this.addCard = function(oCard) {
        self.cards.push(oCard);
    };
    this.removeCard = function(oCard) {
        var indexToRemove = -1;
        for (var i=0; i < self.cards.length; i++ ) {
            if (self.cards[i] === oCard) {
                indexToRemove = i;
                break;
            }
        }
        self.cards.splice(indexToRemove, 1);
    };
};